package cn.byapps.hw.dao;

import cn.byapps.hw.entity.Clazz;
import cn.byapps.hw.entity.Student;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * StudentDao
 */
public interface StudentDao extends JpaRepository<Student, String> {
    /**
     * 根据班级获取所有学生
     *
     * @param pageable
     * @param clazz
     * @return
     */
    @Query("select (u) from Student u where :clazz in elements(u.clazzs) ")
    List<Student> findPageByClazz(Pageable pageable, Clazz clazz);

    /**
     * 统计该班级拥有多少名学生
     *
     * @param clazz
     * @return
     */
    @Query("select count(u) from Student u where :clazz in elements(u.clazzs) ")
    long countByClazz(Clazz clazz);

    /**
     * 根据条件查询（姓名或者学好）
     *
     * @param pageable
     * @param search   条件查询
     * @return
     */
    @Query("select (u) from Student u where u.id LIKE CONCAT('%',:search,'%') or u.name LIKE CONCAT('%',:search,'%')")
    List<Student> findPageByClazz(Pageable pageable, String search);

    /**
     * 根据条件查询统计拥有多少名学生
     *
     * @param search
     * @return
     */
    @Query("select count(u) from Student u where u.id LIKE CONCAT('%',:search,'%') or u.name LIKE CONCAT('%',:search,'%')")
    long countByClazz(String search);
}